package com.example.demo.leetcode.top100;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _10和为K的子数组
 * @description
 * @date 2023/06/05 20:59
 * <p>
 * ******************************************************
 */
public class _10和为K的子数组 {

    private int[] prem;

    public int subarraySum(int[] nums, int k) {
        prem = new int[nums.length + 1];
        prem[1] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            prem[i + 1] = nums[i] + prem[i];
        }

        int res = 0;
        for (int i = 0; i < prem.length - 1; i++) {
            for (int j = i + 1; j < prem.length; j++) {
                if (prem[j] - prem[i] == k) {
                    res++;
                }
            }
        }
        return res;
    }

}
